function SetRecoverRate takes unit whichUnit , real newValue returns nothing
	call SaveReal(udg_UnitDataHashTable , GetHandleId(whichUnit) , StringHash("HP_RECOVER_RATE") , newValue)
endfunction

function Trig_HPRecover_Actions takes nothing returns nothing
	local trigger playerRecoverTrigger = GetTriggeringTrigger()
	local player me = LoadPlayerHandle(udg_TriggerDataHashTable , GetHandleId(playerRecoverTrigger) , StringHash("PLAYER"))
	local integer playerId = GetPlayerId(me)
	local unit survivor = udg_Survivors[playerId]
	local real rate = LoadReal(udg_UnitDataHashTable , GetHandleId(survivor) , StringHash("HP_RECOVER_RATE"))
	local real hpNow = GetUnitState(survivor , UNIT_STATE_LIFE)
	local real hpMax = GetUnitState(survivor , UNIT_STATE_MAX_LIFE)
	local real diff = hpMax - hpNow
	local real rateAcceleration = -0.00293 * (hpNow / hpMax) + 0.00408
	//--
	if GetPlayerController(me) == MAP_CONTROL_USER and GetPlayerSlotState(me) == PLAYER_SLOT_STATE_PLAYING then
		call SetUnitState(survivor , UNIT_STATE_LIFE , hpNow + rate)
		if hpNow >= hpMax then
		elseif diff < rate then
			call SetRecoverRate(survivor , rate)
		else
			call SetRecoverRate(survivor , rate + rateAcceleration)
		endif
	else
		call FlushChildHashtable(udg_TriggerDataHashTable , GetHandleId(playerRecoverTrigger))
		call TriggerRemoveAction(playerRecoverTrigger , LoadTriggerActionHandle(udg_TriggerDataHashTable , GetHandleId(playerRecoverTrigger) , StringHash("ACTION")))
		call DestroyTrigger(playerRecoverTrigger)
	endif
	//--
	set me = null
	set survivor = null
endfunction

function InitTrig_HPRecover takes nothing returns nothing
	local integer index = 0
	local trigger playerRecoverTrigger = null
	local unit survivor = null
    loop
    	if GetPlayerController(Player(index)) == MAP_CONTROL_USER  and GetPlayerSlotState(Player(index)) == PLAYER_SLOT_STATE_PLAYING then
			set survivor = udg_Survivors[index]
			call SetRecoverRate(survivor , 0.1)
			set playerRecoverTrigger = CreateTrigger()
			call SavePlayerHandle(udg_TriggerDataHashTable , GetHandleId(playerRecoverTrigger) , StringHash("PLAYER") , Player(index))
			call SaveTriggerActionHandle(udg_TriggerDataHashTable , GetHandleId(playerRecoverTrigger) , StringHash("ACTION") , TriggerAddAction(playerRecoverTrigger , function Trig_HPRecover_Actions))
        	call TriggerRegisterTimerEvent(playerRecoverTrigger , 0.5 , true)
        endif
        set index = index + 1
        exitwhen index >= 10
    endloop
	set playerRecoverTrigger = null
	set survivor = null
endfunction

